from django.shortcuts import render,HttpResponse
from . import cinema_spider
from cinema_app import models
from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
import json
from movie_app import models as mov_model
# Create your views here.
def index(request):
    pass
    return render(request,'cinema.html')

def showcinema(request):
    pass
    return render(request,'cinema_datail.html')

def spider(request):
    cinema_spider.main_spider()
    return render(request,'cinema_datail.html')

def map_search_cinema(request):

	return render(request, 'tool/map_search_cinema.html')

def position(request):
    place = ''
    context = {'place': place}
    return render(request, 'tool/position.html',context)

def distance(request):
    return render(request, 'tool/distance.html')


#影院数据分页获取
@csrf_exempt
def getcin(request):
    if request.method == "POST":
        pagenum = request.POST.get('pagenum')
        end = int(pagenum) * 10
        first = end - 10
        all = models.cinema.objects.filter().order_by('id')[first:end]
        json_data = serializers.serialize('python',all) #序列化查询出的数据,转成json数据
        cin_list = []
        for item in json_data:
            line = [item['fields']['name'],item['fields']['addr'],item['fields']['label'],item['fields']['tel_phone'],item['fields']['photodir'][1:]]
            cin_list.append(line)
        result = json.dumps(cin_list,ensure_ascii=False)
        return HttpResponse(result)
    #return  HttpResponse()


#影院详情页面数据
def tocinall(request,name,addr):
    try:
        cinema = models.cinema.objects.get(name=name,addr = addr)
        content={}
        content["cin_name"] = cinema.name
        content["cin_imag"] = cinema.photodir[1:]
        content["cin_addr"] = cinema.addr
        content["cin_telphone"] = cinema.tel_phone
        content["cin_label"] = ""
        labellist = cinema.label.split(",")
        for i in range(1,len(labellist)):
            line = '<span style="border-style: solid;border-width: 1px;border-radius: 5px;color: #e84545;margin-left: 10px;">&nbsp;'+labellist[i]+'&nbsp;</span>'
            content["cin_label"] = content["cin_label"] + line
        content["mov_listline"] = ""
        #解锁当前电影院电影信息
        sessall = models.Session_info.objects.values("movie_id").filter(cinema_id=cinema.id ).distinct()

        if len(sessall)>6:
            for i in range(0,6):
                #找电影
                movie = mov_model.Movie_info.objects.get(id = sessall[i]['movie_id'])
                line = '<li id = "'+str(movie.id)+'" class="list-group-item" onclick="changemov(this)"><div class="cinlist"><div class="cine_listimg" style="background-image:url('+movie.main_img+');"></div><div style="margin: auto;text-align: center"><span style="margin: auto;font-weight: bold;">' +movie.name+'</span></div></div></li>'
                content["mov_listline"] = content["mov_listline"] + line
        else:
            for i in range(len(sessall)):
                movie = mov_model.Movie_info.objects.get(id = sessall[i]['movie_id'])
                line = '<li id = "'+str(movie.id)+'" class="list-group-item" onclick="changemov(this)"><div class="cinlist"><div class="cine_listimg" style="background-image:url('+movie.main_img+');"></div><div style="margin: auto;text-align: center"><span style="margin: auto;font-weight: bold;">' +movie.name+'</span></div></div></li>'
                content["mov_listline"] = content["mov_listline"] + line
        content['orderlist'] = ""
        ordlist = models.Session_info.objects.filter(cinema_id=cinema.id).order_by('be_time')
        for i in range(len(ordlist)):
            line1 = '<div class="list_order_iterm row" id = "'+ordlist[i].movie_id+'"style="color: #e84545;font-weight: bold;text-align: center;font-size: 1.3rem;"><div class="col-2">'+ordlist[i].be_time.strftime('%Y-%m-%d %H:%M:%S')[11:16]+'</div><div class="col-2">'+ordlist[i].mov_type+'</div><div class="col-2">'+ordlist[i].Location+'</div><div class="col-2">'+ordlist[i].price+'￥</div><div class="col-4"><div class="btn_B" onclick="buytic('+str(ordlist[i].id)+')" style="cursor: pointer;width: 100px;border-radius: 15px;background-color: #ff8882;color: black;margin: auto;">购票</div></div>   <!--购票按钮--></div><hr id = "'+ordlist[i].movie_id+'" style="background-color: #a7bbc7;height: 1px;">'
            content['orderlist'] = content['orderlist'] + line1

        return render(request,'cinema_datail.html',content)
    except Exception as e:
        print(e)
        return render(request,'404.html')
